package com.hcl.producer;

import lombok.extern.slf4j.Slf4j;
import org.hcl.common.User;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: cailong
 * @Date: 2021/6/4 22:44
 * @Description:
 */
@RestController
@Slf4j
public class ProducerController {

    @Resource
    private DBServiceClient dbServiceClient;

    @Resource
    SendService sendService;


    /**
     * 查找uid用户，如果找到就发送到消息队列中
     * @param uid
     * @return
     */
    @RequestMapping(value = "/sendUser")
    public String sendUser(String uid){
        User user = dbServiceClient.getUser(uid);
        log.info("user:"+user);
        if (user == null) {
            log.warn("fail ! case by uid=" + uid + " from user is null!");
            return "fail ! case by uid=" + uid + " from user is null!";
        }
        //创建消息
        Message msg = MessageBuilder.withPayload(user).build();
        boolean isSend = sendService.myUser().send(msg);
        log.info(isSend==true?"消息已发送到消息队列":"消息发送失败");
        return "SUCCESS!";
    }
}
